/**
 * AspectSense project
 *
 * Middleware for annotation-based, context-aware and adaptive services for
 * mobile and ubiquitous computing
 *
 * This project is an open-source effort, started at the University of Cyprus
 * [http://www.ucy.ac.cy].
 *
 * More information about the project is available at
 * [http://code.google.com/p/aspectsense]
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package org.aspectsense.example_apps.sorted_helloworld;

import org.aspectsense.annotations.Component;
import org.aspectsense.annotations.Service;
import org.aspectsense.annotations.ServiceUtility;
import org.aspectsense.framework.DefaultComponent;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * <p/>
 * Date: Sep 10, 2007<br/>
 * Time: 7:02:46 PM
 * </p>
 *
 * @author Nearchos Paspallis [nearchos@cs.ucy.ac.cy]
 */
@Component(
        displayID = "BubbleSortImpl",
        description = "Implements the bubble-sort algorithm"
)
@Service(
        serviceType = SortingService.class
)
@ServiceUtility(
        targetServiceType = SortingService.class,
        utilityFunction = "context[context.infrastructure.memory-availability]"
)
public class BubbleSortImpl extends DefaultComponent implements SortingService
{
    public List<String> sort(final Set<String> input)
    {
        final int length = input.size();

        final String [] array = new String[length];
        int counter = 0;
        for(String s : input)
        {
            array[counter++] = s;
        }

        for(int i = 0; i < length; i++)
        {
            for(int j = (length - 1); j > i; j--)
            {
                if(array[j].compareTo(array[j - 1]) < 0)
                {
                    // swap
                    String temp = array[j];
                    array[j] = array[j - 1];
                    array[j - 1] = temp;
                }
            }
        }

        return new ArrayList<String>(Arrays.asList(array));
    }
}